<template>
  	<div id="registor">
  		<header>
  			<i @click="goBack()"></i>
  			<div>注册</div>
  		</header>
    	<ul class="registor">
	  		<li>
	  			<a>
					<img src="../../image/icon_phone.png" width="20" height="20" alt="">
		 			<input type="text" placeholder="请输入手机号" v-model="sentMsg.tele"  @input="checkNum" required />
	  			</a>
	  		</li>
	  		<li>
	  			<a>
					<img src="../../image/icon_verification_code.png" width="20" height="20" alt="">
		 			<input type="text" placeholder="请输入验证码" v-model="code" required @click="testPhone" />
		 			<span @click='getCode()' v-show='!hasCode'>获取验证码</span>
		 			<span v-show='hasCode'>重新获取{{count}}s</span>
	  			</a>
	  		</li>
	  		<li>
	  			<a>
					<img src="../../image/icon_new_password.png" width="20" height="20" alt="">
		 			<input type="password" placeholder="请设置密码" v-model="sentMsg.pass" required />
	  			</a>
	  		</li>
	  		<li>
	  			<a>
					<img src="../../image/icon_confirm_password.png" width="20" height="20" alt="">
		 			<input type="password" placeholder="请确认密码" v-model="pass2" required />
	  			</a>
	  		</li>
	  		<li>
	  			<a class="confirmBtn" @click="registorFun">
	  				注 册
	  			</a>
	  		</li>

	  	</ul>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				list: [],
				sentMsg: {
                    tele: '',
					pass: '',
					// tele_type: ''  //交流  /合作  交流+合作
                },
                // tele_type: [],
				pass2: '',
                code: '', //用户填写的验证码
                hasCode: false, //是否有验证码 默认无 发送后一分钟内是true 然后false
                time_count: 60, //倒计时时间
                count: '',  //60s倒计时
                // timer: null,  //计时器
                timer: '',  //计时器
			}
		},
		components: {

		},
		methods: {
			goBack() {
				this.$router.push({path:'/login1'})
			},
            //注册
			registorFun() {
                // if(this.tele_type.length == 2) {
                //     this.sentMsg.tele_type = '交流+合作'
                // }else if (this.tele_type[0] == 1){
                //     this.sentMsg.tele_type = '合作';
                // }else if (this.tele_type[0] == 2){
                //     this.sentMsg.tele_type = '交流';
                // }
                if (!this.sentMsg.tele) {
			        this.$utils._toast("手机号不能为空");
			        return false;
		        } else if (!/^1(3|4|5|7|8|9)\d{9}$/.test(this.sentMsg.tele)) {
		        	this.$utils._toast("手机号码有误，请重填");
		        	return false;
                } 
                else if (!this.code) {
                    this.$utils._toast("验证码不能为空");
		        	return false;
                }
                else if(!this.sentMsg.pass) {
                    this.$utils._toast("密码不可为空");
                    return false;
				} else if(this.sentMsg.pass != this.pass2)  {
                    this.$utils._toast("两次密码不一致");
                    return false;
				} else {
                    console.log(this.code)
                    this.$api.post(this.$api.baseURLSchool,"regist_phone/"+this.code,this.sentMsg,r => {
                        console.log(r);
                        if(r.data.ret == 1) {  //验证码发送成功后开始60s倒计时
                            localStorage.setItem("loginMobile", this.sentMsg.tele);
					    	this.$router.push({name:'login2'})
                        }else if(r.data.ret == -1){
                            this.$utils._toast('验证码错误');
                        }else if(r.data.ret == -2){
                            this.$utils._toast('该手机号已注册');
                        }          
                    },err => {
                        console.log(err);
                    })
                    //注册
                    // this.$api.post(this.$api.baseURLSchool,"regist_phone/"+this.code,this.sentMsg,r => {
                    //     console.log(r);
                    //     if(r.data.ret == 1) {  //验证码发送成功后开始60s倒计时
                    //         localStorage.setItem("loginMobile", this.sentMsg.tele);
					//     	this.$router.push({name:'my'})
                    //     }else if(r.data.ret == -1){
                    //         this.$utils._toast('验证码错误');
                    //     }else if(r.data.ret == -2){
                    //         this.$utils._toast('该手机号已注册');
                    //     }          
                    // },err => {
                    //     console.log(err);
                    // })
                }
			},
			//校验手机号
	        checkNum(e){
		        var value = e.target.value;
		        e.target.value = value.replace(/[^\d]/g, '');
		        this.sentMsg.tele = e.target.value;
	        },
	        aa() {
	        	console.log(this.sentMsg.tele_type);
	        },
	        testPhone(){
		        if (this.sentMsg.tele == "") {
			        this.$utils._toast("手机号不能为空");
			        return false;
		        } else if (!/^1(3|4|5|7|8|9)\d{9}$/.test(this.sentMsg.tele)) {
		        	this.$utils._toast("手机号码有误，请重填");
		        	return false;
		        }
            },
            //点击获取验证码
            getCode() {
                console.log('***+',this.timer)
                if (this.sentMsg.tele == "") {
			        this.$utils._toast("手机号不能为空");
			        return false;
		        } else if (!/^1(3|4|5|7|8|9)\d{9}$/.test(this.sentMsg.tele)) {
		        	this.$utils._toast("手机号码有误，请重填");
		        	return false;
                } else {  //手机号码有效
                    if(!this.timer) {
                        const obj = {phone: this.sentMsg.tele};
                        this.$api.post(this.$api.baseURLSchool,"send_code/regist",obj,r => {
                            console.log(r);
                            if(r.data.ret == 1) {  //验证码发送成功后开始60s倒计时
                                this.count = this.time_count;
                                this.hasCode = true;
                                this.timer = setInterval(() => {
                                    if (this.count > 0 && this.count <= this.time_count) {
                                        this.count--;
                                    } else {
                                        this.hasCode = false;
                                        clearInterval(this.timer);
                                        this.timer = '';
                                    }
                                }, 1000)
                            }else if(r.data.ret == -1){
                                this.$utils._toast('获取验证码频繁，请稍后再试');
                            }            
                        },err => {
                            console.log(err);
                        })
                        
                    }
                    
                }    
            }
		},
		created() {
		},
		mounted() {
		}
	}
</script>
<style lang="scss">
</style>
